﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using FbHelperWP7NonUi.Settings;
using FbHelperWP7NonUi.TextHelpers;
using FbHelperWP7.Controls;

namespace FbHelperWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static readonly Uri textReadPageUri = new Uri( "/Pages/TextReadPage.xaml", UriKind.RelativeOrAbsolute );
        private static readonly Uri reviewPageUri = new Uri( "/Pages/ReviewPage.xaml", UriKind.RelativeOrAbsolute );

        private static readonly ProgramState programState = ProgramState.CurrentState;

        public MainPage()
        {
            InitializeComponent();

            phoneStorageControl.BookSelectedEvent += new PhoneStorageControl.BookSelected( phoneStorageControl_BookSelectedEvent );            
        }

        private void phoneStorageControl_BookSelectedEvent( Book book )
        {
            OpenBook( book );
        }

        private void RecentBookListBoxItemTap( object sender, GestureEventArgs e )
        {
            throw new NotImplementedException();
        }

        private void ReviewListBoxItemTap( object sender, GestureEventArgs e )
        {
            NavigationService.Navigate( reviewPageUri );
        }

        private void OpenBook( Book book )
        {
            NavigationCache.Book = book;

            NavigationService.Navigate( textReadPageUri );
        }

        protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e )
        {
            base.OnNavigatedTo( e );

            if (programState.History.RecentFile != null)
            {
                recentBook.Visibility = System.Windows.Visibility.Visible;
                recentBook.Content = programState.History.RecentFile.BookName;
            }
        }
    }
}